package com.example.weather;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by asd on 2016/9/14.
 */
public class Fragment4 extends Fragment implements RadioGroup.OnCheckedChangeListener {
    private View view;

    private RadioGroup rg;
    private ListView lv;

    private List<News> data;
    private String httpUrl = "http://apis.baidu.com/txapi/keji/keji?num=30";

    //apiKey:6d96b140df1db1b19388daeaec241d05
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment4, null);
        init();
        MyListener();
        return view;
    }

    private void MyListener() {
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent in = new Intent(getActivity(), listActivity.class);
                in.putExtra("url", data.get(position).getUrl());
                startActivity(in);
            }
        });
    }

    //使用xUtils请求网上数据
    private void JieXi() {
        HttpUtils utils = new HttpUtils();
        RequestParams params = new RequestParams();
        params.addHeader("apikey", "6d96b140df1db1b19388daeaec241d05");
        utils.send(HttpRequest.HttpMethod.GET, httpUrl, params, new RequestCallBack<String>() {

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                getTxt(responseInfo.result);
                lv.setAdapter(new MyAdapter());
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
    }

    //初始化控件
    private void init() {
        rg = (RadioGroup) view.findViewById(R.id.rg);
        lv = (ListView) view.findViewById(R.id.lv);
        rg.setOnCheckedChangeListener(this);
        JieXi();
    }

    //解析得到的JSON数据
    public void getTxt(String s) {
        try {
            JSONObject jsonObject = new JSONObject(s);
            JSONArray jsonNewsList = jsonObject.getJSONArray("newslist");

            data = new ArrayList<>();
            for (int i = 0; i < jsonNewsList.length(); i++) {
                News news = new News();
                JSONObject js = (JSONObject) jsonNewsList.get(i);
                news.setcTime(js.getString("ctime"));
                news.setTitle(js.getString("title"));
                news.setPicUrl(js.getString("picUrl"));
                news.setUrl(js.getString("url"));
                data.add(news);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    //radioButton的监听事件
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.KeJi:
                httpUrl = "http://apis.baidu.com/txapi/keji/keji?num=30";
                JieXi();
                break;
            case R.id.apple:
                httpUrl = "http://apis.baidu.com/txapi/apple/apple?num=30";
                JieXi();
                break;
            case R.id.inter:
                httpUrl = "http://apis.baidu.com/txapi/world/world?num=30";
                JieXi();
                break;
            case R.id.play:
                httpUrl = "http://apis.baidu.com/txapi/tiyu/tiyu?num=30";
                JieXi();
                break;
        }
    }

    //listView的适配器
    private class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            BitmapUtils bitmapUtiles = new BitmapUtils(getActivity());
            ViewHolder holder = new ViewHolder();
            if (convertView == null) {
                convertView = LayoutInflater.from(getActivity()).inflate(R.layout.adapter_layout, null);
                holder.iv_pic = (ImageView) convertView.findViewById(R.id.iv_pic);
                holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
                holder.tv_title = (TextView) convertView.findViewById(R.id.tv_Title);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.tv_title.setText(data.get(position).getTitle());
            holder.tv_time.setText(data.get(position).getcTime());
            bitmapUtiles.display(holder.iv_pic, data.get(position).getPicUrl());
            return convertView;
        }

        private class ViewHolder {
            TextView tv_title;
            ImageView iv_pic;
            TextView tv_time;
        }
    }
}
